/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.labmetrol.entities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Felipe
 */
public class FileManager {

    private FileInputStream fileInputSteam;
    private FileOutputStream fileOutputSteam;

    public FileManager() throws FileNotFoundException {
    }

    public void writeLoadCell(LoadCell o) throws IOException {
        new File("Celulas").mkdir();
        fileOutputSteam = new FileOutputStream("Celulas/" + o.getIdentification());
        ObjectOutputStream objectOutStream = new ObjectOutputStream(fileOutputSteam);
        objectOutStream.writeObject(o);
        objectOutStream.flush();
        objectOutStream.close();
        fileOutputSteam.flush();
        fileOutputSteam.close();
    }

    public String[] getAllCells() {
        File dir = new File("Celulas");
        String[] children = dir.list();
        return children;
    }
    
    public void removeCell(String cell) throws FileNotFoundException, IOException, ClassNotFoundException {
        String directory = "Celulas/" + cell;
        File f = new File(directory);
        f.delete();
    }
    
    public LoadCell getCellById(String identification) throws FileNotFoundException, IOException, ClassNotFoundException {
        fileInputSteam = new FileInputStream("Celulas/" + identification);
        ObjectInputStream objectInStream = new ObjectInputStream(fileInputSteam);
        LoadCell loadCell = (LoadCell) objectInStream.readObject();
        return loadCell;
    }
}
